package com.work.servlet;

import com.work.dao.UserDao;
import com.work.dao.UserDaoImpl;
import com.work.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns="/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserDao userDao = new UserDaoImpl();
        User user = userDao.login(username, password);
        //账号密码正确
        if (user!=null){
            HttpSession session = request.getSession();
            session.setAttribute("state",user);

//            登陆成功 跳转首页
            request.getRequestDispatcher("/home.jsp").forward(request,response);
            return;
        }
//        登陆失败 返回首页
        response.sendRedirect("/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       super.doGet(request,response);
    }
}
